<?php
/*=============================================================================
|| ##################################################################
||	phpFoX Konsort
|| ##################################################################
||
||	Copyright		: (C) 2005-2007 PHPFOX Limited a Reality Fox Creation
||	Contact			: info@phpfox.com
||
||	- phpFoX and all of its source code and files are protected by Copyright Laws.
||
||	- The license for phpFoX permits you to install this software on a single domain (.com, .org, .net, etc.).
||
||	- You may also not remove this copyright screen which shows the copyright information and credits for phpFoX (RealityFOX Creations).
||
||	- phpFoX Konsort is NOT a FREE software - http://www.phpfox.com/license/
||
|| ##################################################################
=============================================================================*/

/* $File: ./include/modules/Music/classes/PhpFox_ComponentUploadSong.class.php, $Author:  $ */



/** Component for uploading songs
 *
 * @version $Id: PhpFox_ComponentUploadSong.class.php 98 2007-12-17 08:48:54Z phpfoxsv $
 * @package module.music
 */
class PhpFox_Mod_Music_ComponentUploadSong extends BaseComponent
{
    function process()
    {
        $oMusicService = &$this->_oModule->getService('Music');
        /* @var $oMusicService PhpFox_Mod_Music_ServiceMusic */
        $oSecurityService = &App::getModuleService('Account', 'Security');
        /* @var $oSecurityService PhpFox_Mod_Account_ServiceSecurity */

        $oAccountService = &App::getModuleService('Account', 'Account');

        $oReq = &App::getRequest();
        $oTpl = &$this->_oModule->getTemplate();
        App::loadClass('Validator');

        $oSecurityService->checkPerm('music', array('full'));
        $oSecurityService->checkAccess('music');
        $aFields = array
        (
            'val[song_title]'    => array('title' => App::format('song_title'),    'def' => 'required'),
            'val[musician_name]' => array('title' => App::format('musician_name'), 'def' => 'required'),
        );
        $oValidator = &new Validator($aFields);
        if ($oReq->getAction() == 'upload')
        {
            $aValues = $oReq->getArray('val');
            if (!$oValidator->isValid($oReq->getAll()))
            {
                $this->addErrors($oValidator->getErrors());
            }
            if ( substr($_FILES['song']['name'], -3) != 'mp3' )
            {
            	$this->addErrors(App::format('Mod_Music.error.wrong_format'));
            }
            if (!count($this->getErrors()))
            {
                $oCurrentUser = $oSecurityService->getCurrentUser();
                $aSongData = array
                (
                    'song_title'    => htmlspecialchars($aValues['song_title']),
                    'musician_name' => htmlspecialchars($aValues['musician_name']),
                    'genre_id'      => $aValues['genre_id'],
                    'album_id' => $aValues['album_id'],
                );

                $iUsedSpace = $oAccountService->getUsedSpace($oSecurityService->getCurrentUserLogin());
                $iAllowedSpace = $oAccountService->getAllowedSpace($oSecurityService->getCurrentUser());
                $iMaxUploadSize = App::getParam('Mod_Music.max_song_size');
                if (($iAllowedSpace != 0) && (($iAllowedSpace - $iUsedSpace) < $iMaxUploadSize))
                {
                    $iMaxUploadSize = $iAllowedSpace - $iUsedSpace;
                }

                if (!$oMusicService->uploadSong('song', $aSongData, $oCurrentUser->aData['id'], $iMaxUploadSize))
                {
                    $this->addErrors($oMusicService->getErrors());
                }
            }
            if (count($this->getErrors()))
            {
            	App::gotoUrl('', array(), $this->getErrors(), true);
            }
            else 
            {
            	App::gotoUrl('', array(), App::format('Mod_Music.info.song_uploaded'), false);
            }
            $oTpl->assign(array(
                'aValue' => $aValues,
            ));
        }

        $aAlbums = $this->oDatabase->getRows("SELECT album_id, album_title FROM ". App::getT('music_album') ." WHERE user_id = '". App::getUser("id") ."'");

        if ( !count($aAlbums) )
        {
        	App::gotoUrl('public.account.submit.music.album', array());
        }

        $oTpl->assignSrc(array(
            'sValidatorJs'   => $oValidator->makeJS(),
            'sValidatorMess' => $oValidator->makeJsMess(),
        ));
        $oTpl->assign(array(
            'aGenres'      => $oMusicService->getGenres(),
            'iMaxSongSize' => App::getParam('Mod_Music.max_song_size'),
            'aAlbums' => $aAlbums
        ));
        return $oTpl->fetch('UploadSong.html');
    }
}
?>